package com.atguigu.paymentdemo.service.impl;

import com.atguigu.paymentdemo.entity.OrderInfo;
import com.atguigu.paymentdemo.entity.Product;
import com.atguigu.paymentdemo.enums.OrderStatus;
import com.atguigu.paymentdemo.mapper.OrderInfoMapper;
import com.atguigu.paymentdemo.mapper.ProductMapper;
import com.atguigu.paymentdemo.service.OrderInfoService;
import com.atguigu.paymentdemo.util.OrderNoUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.Instant;
import java.util.List;

@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {

    @Resource
    private ProductMapper productMapper;

    @Override
    public OrderInfo createOrderByProductId(Long productId) {
        Product product = productMapper.selectById(productId);
        OrderInfo orderInfo = selectOrder(productId);
        if (orderInfo == null) {
            OrderInfo info = new OrderInfo();
            info.setTitle(product.getTitle());
            info.setOrderNo(OrderNoUtils.getOrderNo());
            info.setProductId(productId);
            info.setTotalFee(product.getPrice());
            info.setOrderStatus(OrderStatus.NOTPAY.getType());
            this.baseMapper.insert(info);
            return info;
        }
        return orderInfo;
    }

    @Override
    public void saveCodeUrl(String orderNo, String codeUrl) {
        //保存订单支付二维码
        QueryWrapper<OrderInfo> wrapper = new QueryWrapper<OrderInfo>().eq("order_no", orderNo);
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setCodeUrl(codeUrl);
        this.baseMapper.update(orderInfo, wrapper);
    }

    @Override
    public List<OrderInfo> listOrderByCreateTimeDesc() {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public String getOrderStatus(String out_trade_no) {

        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", out_trade_no);
        OrderInfo orderInfo = this.baseMapper.selectOne(queryWrapper);
        if (orderInfo != null) {
            return orderInfo.getOrderStatus();
        }
        return null;
    }

    /**
     * 查询超过i分钟并且未支付的订单
     *
     * @param i
     * @return
     */
    @Override
    public List<OrderInfo> getNoPayOrderByOuration(int i) {
        Instant now = Instant.now();
        Instant minus = now.minus(Duration.ofMinutes(i));  //五分钟之前的时间
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_status", OrderStatus.NOTPAY.getType());
        queryWrapper.le("create_time", minus);
        List<OrderInfo> list = this.baseMapper.selectList(queryWrapper);
        return list;
    }

    /**
     * 根据订单号获取订单
     *
     * @param orderNo
     * @return
     */
    @Override
    public OrderInfo getOrderByOrderNo(String orderNo) {

        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", orderNo);
        OrderInfo orderInfo = baseMapper.selectOne(queryWrapper);

        return orderInfo;
    }


    public OrderInfo selectOrder(Long productId) {
        //判断订单是否存在，存在就用存在的，不再创建（幂等处理）
        QueryWrapper<OrderInfo> wrapper = new QueryWrapper<OrderInfo>().eq("product_id", productId).eq("order_status", OrderStatus.NOTPAY.getType());
        //说明有订单
        return this.baseMapper.selectOne(wrapper);
    }
}
